<?php
$error = array(
	"col" => "#CCC",
	"to" => "",
	"sub" => "",
	"con" => ""
	);
if (isset($_POST["send"])){
	// Looking for the receiver in the DB.
	$arr = array("*");	
	$cond = array(
		"user"			=>	$_POST["to"]
		);
	$result = $DB->query("select", "user", $arr, $cond) or die("Error in DB: " .$DB->error());
	if ($row = $DB->fetch_array($result)){//Sending message
		$arr = array("id");
		$cond = array (
			"sender"		=>	$_SESSION["user"]["name"],
			"receiver"		=>	$_POST["to"],
			"subject"		=>	$_POST["subject"],
			"text"			=>	$_POST["content"]
			);
		$result = $DB->query("select", "messages", $arr, $cond) or die("Error in DB: " .$DB->error());
		if (!($fetch = $DB->fetch_array($result))){
			echo "hola",
			$arr = array(
				"sender" => $_SESSION["user"]["name"],
				"receiver" => $row["user"],
				"time" => time(),
				"type" => 0,
				"subject" => $_POST["subject"],
				"text" => $_POST["content"]
				);	
			$result = $DB->query("insert", "messages", $arr) or die("Error in DB: " . $DB->error());
		}
	}
	else{
		$error = array(
			"col" => "red",
			"to" => $_POST["to"],
			"sub" => $_POST["subject"],
			"con" => $_POST["content"]
			);
	}
}
//paginado received
if (isset($_GET["rec_page"])){
	$rec_page = $_GET["rec_page"];
}
else {
	$rec_page = 1;
}
$limit = 10;
$paginate = array($limit * ($rec_page - 1), $limit);
$arr = array("*");	
$cond = array(
	"receiver"			=>	$_SESSION["user"]["name"],
	"sender!"			=> 	$_SESSION["user"]["name"]
	);
$query = $DB->get_query("select", "messages", $arr, $cond, $paginate) or die("Error in DB: " . $DB->error());
$result = $DB->simple_query(str_replace(" LIMIT", " ORDER BY time DESC LIMIT", $query));
$rows_received = $DB->complete_fetch_array($result);

$query = $DB->query("select", "messages", $arr, $cond) or die("Error in DB: " . $DB->error());
$rows_total_received = $DB->complete_fetch_array();
$num_total_received = $DB->get_num_rows();
$rec_page_max = $num_total_received / $limit;
	
if ($rec_page > 1)
	$if_rec_page_minus = $rec_page - 1; 
else 
	$if_rec_page_minus = $rec_page;
if ($rec_page < $rec_page_max)
	$if_rec_page_plus = $rec_page + 1; 
else 
	$if_rec_page_plus = $rec_page;
		
$cond = array(
	"unread" 			=> 	1,
	"receiver"			=> 	$_SESSION["user"]["name"],
	"sender!"			=> 	$_SESSION["user"]["name"]
	);
$result = $DB->query("select", "messages", $arr, $cond) or die("Error in DB: " . $DB->error());
$rows_unread = $DB->complete_fetch_array();
$unreaded = $DB->get_num_rows();
		
//paginado sent
if (isset($_GET["sent_page"])){
	$sent_page = $_GET["sent_page"];
}
else {
	$sent_page = 1;
}
$paginate = array($limit * ($sent_page - 1), $limit);
	
$cond = array(
	"sender"			=>	$_SESSION["user"]["name"],
	"receiver!"			=>	$_SESSION["user"]["name"]
	);
$query = $DB->get_query("select", "messages", $arr, $cond, $paginate) or die("Error in DB: " . $DB->error());
$result = $DB->simple_query(str_replace(" LIMIT", " ORDER BY time DESC LIMIT", $query));
$rows_sent = $DB->complete_fetch_array($result);
		
$result = $DB->query("select", "messages", $arr, $cond) or die("Error in DB: " . $DB->error());
$rows_total_sent = $DB->complete_fetch_array();
$num_total_sent = $DB->get_num_rows();
$sent_page_max = $num_total_sent / $limit;
if ($sent_page > 1)
	$if_sent_page_minus = $sent_page - 1; 
else 
	$if_sent_page_minus = $sent_page;
if ($sent_page < $sent_page_max)
	$if_sent_page_plus = $sent_page + 1; 
else 
	$if_sent_page_plus = $sent_page;

//paginado notes
if (isset($_GET["notes_page"])){
	$notes_page = $_GET["notes_page"];
}
else {
	$notes_page = 1;
}
$paginate = array($limit * ($notes_page - 1), $limit);
	
$cond = array(
	"sender"			=>	$_SESSION["user"]["name"], 
	"receiver"			=>	$_SESSION["user"]["name"]
	);
$query = $DB->get_query("select", "messages", $arr, $cond, $paginate) or die("Error in DB: " . $DB->error());
$result = $DB->simple_query(str_replace(" LIMIT", " ORDER BY time DESC LIMIT", $query));
$rows_notes = $DB->complete_fetch_array($result);
		
$result = $DB->query("select", "messages", $arr, $cond) or die("Error in DB: " . $DB->error());
$rows_total_notes = $DB->complete_fetch_array();
$num_total_notes = $DB->get_num_rows();
$notes_page_max = $num_total_notes / $limit;
if ($notes_page > 1)
	$if_notes_page_minus = $notes_page - 1; 
else 
	$if_notes_page_minus = $notes_page;
if ($notes_page < $notes_page_max)
	$if_notes_page_plus = $notes_page + 1; 
else 
	$if_notes_page_plus = $notes_page;
		
//html messages
$received = "";
foreach ($rows_received as $key => $value){
	$arr = array("citizenship");	
	$cond = array(
		"user"			=>	$value["sender"],
		);
	$result = $DB->query("select", "user", $arr, $cond) or die("Error in DB: " . $DB->error());
	$citizenship = $DB->complete_fetch_array();
	if($value["unread"] == 1){
		$change = <<< texto
onClick="window.open(this.href, this.target, 'width=400,height=200,location=0'); return false;", onmouseover="tdname_1.style.color='red', tdname_2.style.color='red', tdname_3.style.color='red'" onmouseout="tdname_1.style.color='yellow', tdname_2.style.color='yellow', tdname_3.style.color='yellow'">
texto;
		$received .= "<tr>
<td><div class='received'><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_1' " . str_replace("tdname", "tdr" . $key, $change) . date('Y-m-d, H:i', $value["time"]) . "</a></div></td>
<td><div class='received'><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_2' "  . str_replace("tdname", "tdr" . $key, $change) . $value["subject"] . "</a></div></td>
<td><div class='received'><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_3' " . str_replace("tdname", "tdr" . $key, $change) . $value["sender"] . "</a></div></td>
<td><img src='styles/images/flags_gif/".$citizenship[0][0].".gif'></td>
</tr>";
	}
	else {
		$change = <<< texto
onclick="window.open(this.href, this.target, 'width=400,height=200,location=0'); return false;", onmouseover="tdname_1.style.color='red', tdname_2.style.color='red', tdname_3.style.color='red'" onmouseout="tdname_1.style.color='#CCC', tdname_2.style.color='#CCC', tdname_3.style.color='#CCC'">
texto;
		$received .= "<tr>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_1' " . str_replace("tdname", "tdr" . $key, $change) . date('Y-m-d, H:i', $value["time"]) . "</a></td>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_2' "  . str_replace("tdname", "tdr" . $key, $change) . $value["subject"] . "</a></td>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=received' id='tdr" . $key . "_3' " . str_replace("tdname", "tdr" . $key, $change) . $value["sender"] . "</a></td>
<td><img src='styles/images/flags_gif/".$citizenship[0][0].".gif'></td>
</tr>";
				}
			}
$sent = "";
foreach ($rows_sent as $key => $value){
	$arr = array("citizenship");	
	$cond = array(
		"user"			=>	$value["receiver"],
		);
	$result = $DB->query("select", "user", $arr, $cond) or die("Error in DB: " . $DB->error());
	$citizenship = $DB->complete_fetch_array();
	$change = <<< texto
onclick="window.open(this.href, this.target, 'width=400,height=200,location=0'); return false;", onmouseover="tdname_1.style.color='red', tdname_2.style.color='red', tdname_3.style.color='red'" onmouseout="tdname_1.style.color='#CCC', tdname_2.style.color='#CCC', tdname_3.style.color='#CCC'">
texto;
	$sent .= "<tr>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=sent' id='tds" . $key . "_1' " . str_replace("tdname", "tds" . $key, $change) . date('Y-m-d, H:i', $value["time"]) . "</a></td>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=sent' id='tds" . $key . "_2' "  . str_replace("tdname", "tds" . $key, $change) . $value["subject"] . "</a></td>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=sent' id='tds" . $key . "_3' " . str_replace("tdname", "tds" . $key, $change) . $value["receiver"] . "</a></td>
<td><img src='styles/images/flags_gif/".$citizenship[0][0].".gif'></td>
</tr>";
}
$notes = "";
foreach ($rows_notes as $key => $value){
	$change = <<< texto
onclick="window.open(this.href, this.target, 'width=400,height=200,location=0'); return false;", onmouseover="tdname_1.style.color='red', tdname_2.style.color='red'" onmouseout="tdname_1.style.color='#CCC', tdname_2.style.color='#CCC'">
texto;
	$notes .= "<tr>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=notes' id='tdn" . $key . "_1' " . str_replace("tdname", "tdn" . $key, $change) . date('Y-m-d, H:i', $value["time"]) . "</a></td>
<td><a href='home.php?page=view_mes&id=".$value["id"]."&type=notes' id='tdn" . $key . "_2' "  . str_replace("tdname", "tdn" . $key, $change) . $value["subject"] . "</a></td>
</tr>";
}
		
include ("/../styles/html/home/messages/messages.php");
?>